<?php
namespace app\index\controller;
use app\common\exception\System as SystemException;
use Exception;
use app\index\model\Imgtext as model;
use app\index\model\Video as Videos;
use app\index\model\Friend;
use app\index\model\Smallvideo;
use app\index\model\Comment;
use app\index\model\Newsfollow;
use app\index\model\Newscollect;
use app\index\model\Newspraise;
class News extends Common
{
        /**
         * 不需要登录
         * @var array
         */
        protected $noNeedLogin = ['*'];
        /**
         * 新闻首页
         * @return [type] [description]
         */

        public function index(){
          $param = $this->request->param();
          if($param['type']==1){
            $news1=model::field('id,member_id,is_top,title,thumb,create_time')->where("is_top",1)->order("create_time desc")->limit(($param['page']-1)*5,5)->select()->toArray();
            $news2=model::field('id,member_id,is_top,title,thumb,create_time')->where("is_top",0)->order("create_time desc")->limit(($param['page']-1)*5,5)->select()->toArray();
            $news=array_merge($news1,$news2);
            if(!empty($news)){
              $arr=["status"=>1,"data"=>$news,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }else{
              $arr=["status"=>0,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }
          }else if($param['type']==2){
            $video=Videos::field("id,member_id,title,type,url,thumb")->order("create_time desc")->limit(($param['page']-1)*10,10)->select()->toArray();
            if(!empty($video)){
              $arr=["status"=>1,"data"=>$video,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }else{
              $arr=["status"=>0,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }
          }else if($param['type']==3){
            $friend1 = Friend::with('Members')->field("id,member_id,thumb,nickname,sex,hobby,birth")->where("is_top",1)->order("create_time desc")->limit(($param['page']-1)*10,10)->select()->toArray();
            $friend2 = Friend::with('Members')->field("id,member_id,thumb,nickname,sex,hobby,birth")->where("is_top",0)->order("create_time desc")->limit(($param['page']-1)*10,10)->select()->toArray();
            $friend=array_merge($friend1,$friend2);
            if(!empty($friend)){
              $arr=["status"=>1,"data"=>$friend,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }else{
              $arr=["status"=>0,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }
          }else{
            $smallvideo=Smallvideo::field("id,member_id,title,url,thumb")->order("create_time desc")->limit(($param['page']-1)*10,10)->select()->toArray();
            if(!empty($smallvideo)){
              $arr=["status"=>1,"data"=>$smallvideo,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }else{
              $arr=["status"=>0,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }
          }
        }
        /**
         * 新闻详情
         * @param strings $type 新闻类型
         * @param int $id 新闻ID
         * @return array 新闻内容数组
         */
        public function details(){

      	  $id = $this->request->param("id"); 
      	  $member_id=$this->auth->member_id;

    		// $id =41; 
      // 	  $member_id=2;

          $news=model::field('id,member_id,title,details,create_time')->where("id",$id)->find()->toArray();

			

    		if(is_null($member_id)){
				$news['follow']=0;
    		}else{
    			$news['follow']=$this->newsFollow($news['member_id'],$member_id);
    		}

			if(is_null($member_id)){
				$news['collect']=0;
    		}else{
    			$news['collect']=$this->newsCollect($id,$member_id);
    		}


    		if(is_null($member_id)){
				$news['praise']=0;
    		}else{
    			$news['praise']=$this->Newspraise($id,$member_id);
    		}
          

          $res=Comment::where([["news_id","=",$id],["type","=",1]])->field("id,news_id,member_id,parent_id,comment_id,content,create_time")->order("id asc")->select()->toArray();
          //dump($res);
          foreach($res as $key=>$value){
          	$whe['news_id']=$value['news_id'];
          	$whe['member_id']=$member_id;
          	$whe['comment_id']=$value['id'];
          	$comment_praise=Newspraise::where($whe)->find();
          	if($comment_praise){
      			$res[$key]['comment_praise']=1;
      		}else{
      			$res[$key]['comment_praise']=0;
      		}
          }
          $Comment=$this->getTree($res);


          if(!empty($news)){
              $arr=["status"=>1,"data"=>$news,'Comment'=>$Comment,"msg"=>"获取成功"];
              // dump($arr);
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }else{
              $arr=["status"=>0,"msg"=>"获取成功"];
              return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
            }
        }
        public function getTree($data,$pid = 0){
            $tree = [];
            foreach($data as $value){
                if($value['comment_id'] == $pid){
                    $value['child'] = $this->getTree($data,$value['id']);
                    $tree[] = $value;
                }
            }
            return $tree;
        }
        /**
         * 支付
         * @param stings $type 支付种类
         */
        public function pay()
        {
              $param = $this->request->param(true);
              $type = trim($param['mtype']??'imgtext');
              $res = service('index/News')->payNewsPrice($type,$param??[]);
              return $this->successReturn($res,SystemException::E_SUCCESS,'支付成功');
        }
        /**
         * 关注文章
         *
         */
        public function addfollow(){
        	$Newsfollow=new Newsfollow;
        	$param['follow_member_id'] = $this->request->request("follow_member_id");
        	$param['member_id']=$this->auth->member_id;
        	
        	$res=Newsfollow::where([
      			['follow_member_id',"=",$param['follow_member_id']],
      			['member_id',"=",$param['member_id']]
      			])->find();
        	if($res){
		    	$result=Newsfollow::where($param)->delete();
		    	if($result){
		    		$arr=['status'=>1,'msg'=>'取消关注'];
		      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
			  	}else{
				  	$arr=['status'=>3,'msg'=>'取消失败'];
			      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
			  	}
		    }else{
		    	$result=$Newsfollow->allowField(true)->save($param);
		    	if($result){
		    		$arr=['status'=>2,'msg'=>'关注成功'];
		      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
			  	}else{
				  	$arr=['status'=>4,'msg'=>'关注失败'];
			      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
			  	}
		    }
        }
      	/*
      	关注田鑫
      	*/
      	public function newsFollow($follow_member_id,$member_id){
      		$res=Newsfollow::where([
      			['follow_member_id',"=",$follow_member_id],['member_id',"=",$member_id]
      			])->find();
      		if($res){
      			return 1;
      		}else{
      			return 0;
      		}
      	}
      	public function newsCollect($news_id,$member_id){
      		$res=Newscollect::where([
      			['news_id',"=",$news_id],['member_id',"=",$member_id]
      			])->find();
      		if($res){
      			return 1;
      		}else{
      			return 0;
      		}
      	}


      	public function Newspraise($news_id,$member_id){
      		$res=Newspraise::where([
      			['news_id',"=",$news_id],['member_id',"=",$member_id],['comment_id',"=",0]
      			])->find();
      		if($res){
      			return 1;
      		}else{
      			return 0;
      		}
      	}
      	public function addNews(){
      		$data=$this->request->param();
             $data['member_id']=$this->auth->member_id;
             $model = new \app\index\model\Imgtext;
             $res=$model->allowField(true)->save($data);
             if($res){
                 $res=['status'=>1,'msg'=>'提交成功'];
                 return $this->successReturn($data,SystemException::E_SUCCESS, $res['msg']); 
           }else{
                 $res=['status'=>0,'msg'=>'提交失败'];
                 return $this->successReturn($res,SystemException::E_SUCCESS, $res['msg']);      
           }
     }
}